package com.example.kotlin_retrofit

import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.kotlin_retrofit.adapter.ArticleAdapter
import com.example.kotlin_retrofit.adapter.UserAdapter
import com.example.kotlin_retrofit.entity.Article
import com.example.kotlin_retrofit.entity.ArticleData
import com.example.kotlin_retrofit.entity.User
import com.example.kotlinandroid.http.Result
import com.example.kotlinandroid.http.HttpHelper
import com.example.kotlinandroid.http.SingleCallback
import retrofit2.Call

class MainActivity : AppCompatActivity() {
    private val mUserList: MutableList<User> = ArrayList()
    private var mArticleList: MutableList<ArticleData> = ArrayList();

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        //或者这里可以不用findViewById,直接用布局中的id作为变量名也可以
        var rvArticleData: RecyclerView = findViewById(R.id.rv_article_data)
        var rvUserData: RecyclerView = findViewById(R.id.rv_user_data)
        rvArticleData.layoutManager = LinearLayoutManager(this)
        rvUserData.layoutManager = LinearLayoutManager(this)
        rvArticleData.adapter = ArticleAdapter(this, mArticleList)
        var userAdapter = UserAdapter(mUserList)
        userAdapter.setOnUserClickListener(object : UserAdapter.OnUserClickListener {
            override fun click(text: String?) {
                Toast.makeText(applicationContext, text, Toast.LENGTH_SHORT).show()
            }
        })
        rvUserData.adapter = userAdapter

        //请求微信公众号接口
        var api = HttpHelper.getApi()
        api!!.getwxarticle()!!.enqueue(object : SingleCallback<Result<List<User>>>() {
            override fun onSuccess(response: Result<List<User>>) {
                if (response.data != null && (response.data?.size ?: 0 > 0)) {
                    mUserList.addAll(response.data!!)
                    rvUserData.adapter?.notifyDataSetChanged()
                }
            }
        })
        //请求文章接口
        var api1 = HttpHelper.getApi()
        api1!!.getArticle("1", "60")!!.enqueue(object : SingleCallback<Result<Article>>() {
            override fun onSuccess(response: Result<Article>) {
                if (response.data != null && response.data?.datas != null && response.data?.datas?.size ?: 0 > 0) {
                    mArticleList.addAll(response.data?.datas!!)
                    rvArticleData.adapter?.notifyDataSetChanged()
                }
            }
        })
    }
}
